package com.dongbao.dialog;

import com.alibaba.fastjson.JSONObject;
import com.dongbao.date.DateCentre;
import com.dongbao.utils.HttpClientUtils;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.ValidationInfo;
import com.sun.istack.Nullable;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class InputCodeDialog extends DialogWrapper {
    private JPanel dialogPanel=new JPanel();
    private JTextField textField1;
    private JTextField textField2;
    private JCheckBox isDefault;

    private JTextField contentInput = new JTextField();
    private JTextField hostInput = new JTextField();
    private JTextField sysCodeInput = new JTextField();
    private JTextField codeInput = new JTextField();
    private JCheckBox isDefaultCheckBox = new JCheckBox();

    private String selectedText;

    public JTextField getHostInputInput() {
        return hostInput;
    }

    public JTextField getSysCodeInput() {
        return sysCodeInput;
    }

    public JTextField getCodeInput() {
        return codeInput;
    }

    public JCheckBox getisDefaultCheckBox() {
        return isDefaultCheckBox;
    }

    public InputCodeDialog(String title,String selectedText) {
        super(true);
        init();
        setTitle(title);
        this.selectedText = selectedText;
        this.contentInput.setText(selectedText);
        /* 判断是否已经设置服务地址 如果存在则直接调用查询现有多语言 */
        if(null != DateCentre.hsot){
            /* 调用获取检测接口 */
            try{
                checkSelectedText();
            }catch (Exception ex){
                ValidationInfo validationInfo = new ValidationInfo("请检查配置的地址是否正确【"+DateCentre.hsot+"/sysLocalesInfo/ideaQuerySysLog】");
                Messages.showMessageDialog(validationInfo.message,"提示:", Messages.getInformationIcon());
            }
        }
    }

    private void checkSelectedText(){
        StringBuffer url = new StringBuffer();
        url.append(DateCentre.hsot);
        url.append("/sysLocalesInfo/ideaCheckLocalesInfo?");
        try {
            url.append("content=" + URLEncoder.encode(selectedText, "UTF-8"));
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        }
        JSONObject jsonObject = HttpClientUtils.httpGet(url.toString());
        if(null != jsonObject){
            String code = jsonObject.getString("code");
            if("200".equals(code)){
                /* 填充获取到的内容到输入框内容中 */
                JSONObject rowsObj = jsonObject.getJSONObject("rows");
                /* 获取系统 CODE */
                String systemCode = rowsObj.getString("systemCode");
                /* 获取多语言 CODE */
                String langCode = rowsObj.getString("code");

                this.codeInput.setText(langCode);
                this.sysCodeInput.setText(systemCode);
            }else{
                /* 没有存在的或者服务不通 啥都不用做 */

            }
        }

    }

    @Nullable
    @Override
    protected JComponent createCenterPanel() {
//        JPanel dialogPanel = new JPanel(new BorderLayout());
//        dialogPanel.setLayout(new GridLayout());
//        dialogPanel.setLayout(new GridLayout(5, 2,20,10));
        dialogPanel.setLayout(new GridLayout(5, 2,20,10));
        dialogPanel.setPreferredSize(new Dimension(500,200));

        JLabel contentInputLabel=new JLabel();
        contentInputLabel.setPreferredSize(new Dimension(100, 28));
        contentInputLabel.setText("选择的文本：");
        dialogPanel.add(contentInputLabel);

        contentInput.setPreferredSize(new Dimension(400, 28));
        contentInput.setToolTipText("");
        contentInput.setEnabled(true);
        dialogPanel.add(contentInput);

        JLabel hostInputLabel=new JLabel();
        hostInputLabel.setPreferredSize(new Dimension(100, 28));
        hostInputLabel.setText("DHG服务地址：");
        dialogPanel.add(hostInputLabel);

        hostInput.setPreferredSize(new Dimension(400, 28));
        hostInput.setToolTipText("请输入DHG服务地址");
        if(null != DateCentre.hsot && !"".equals(DateCentre.hsot)){
            hostInput.setText(DateCentre.hsot);
        }
        dialogPanel.add(hostInput);

        JLabel codeInputLabel=new JLabel();
        codeInputLabel.setPreferredSize(new Dimension(100, 28));
        codeInputLabel.setText("多语言Code：");
        dialogPanel.add(codeInputLabel);

        codeInput.setPreferredSize(new Dimension(400, 28));
        codeInput.setToolTipText("请输入多语言Code");
        dialogPanel.add(codeInput);

        JLabel sysCodeInputLabel=new JLabel();
        sysCodeInputLabel.setPreferredSize(new Dimension(100, 28));
        sysCodeInputLabel.setText("系统模块Code：");
        dialogPanel.add(sysCodeInputLabel);

        sysCodeInput.setPreferredSize(new Dimension(400, 28));
        sysCodeInput.setToolTipText("请输入多语言系统模块Code");
        if(null != DateCentre.sysCode && !"".equals(DateCentre.sysCode)){
            sysCodeInput.setText(DateCentre.sysCode);
        }
        dialogPanel.add(sysCodeInput);

        isDefaultCheckBox.setPreferredSize(new Dimension(400, 28));
        isDefaultCheckBox.setText("是否默认生成");
        dialogPanel.add(isDefaultCheckBox);
        return dialogPanel;
    }

    @Nullable
    @Override
    public ValidationInfo doValidate() {
        String hostText = hostInput.getText();
        if (StringUtils.isBlank(hostText)) {
            return new ValidationInfo("输入服务地址不能为空");
        }
        if(this.isDefaultCheckBox.isSelected()){
            return null;
        }
        String codeCext = codeInput.getText();
        if (StringUtils.isBlank(codeCext)) {
            return new ValidationInfo("输入多语言Code不能为空");
        }
        String sysCodeText = sysCodeInput.getText();
        if (StringUtils.isBlank(sysCodeText)) {
            return new ValidationInfo("输入多语言模块Code不能为空");
        }
        return null;
    }
}
